<?php
namespace mobadmin\service;

use herosphp\core\Log;
use mobadmin\service\interfaces\IManagerService;
use common\service\CommonService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\session\Session;
use herosphp\web\WebUtils;
use herosphp\string\StringUtils;
use herosphp\utils\ArrayUtils;
use api\utils\SaasApiClient;

Loader::import('mobadmin.service.interfaces.IManagerService');

/**
 * admin(Service)接口实现
 * @package admin\service
 * @author yu
 */
class ManagerService extends CommonService implements IManagerService
{

    /**
     * 微信授权的方式登录
     * @param $wxUserInfo  微信授权登录，获取的用户信息
     * @return mixed
     */
    public function login($wxUserInfo)
    {
        Log::info("开始 执行 api/user/createUserByWeiXin");

        //用微信授权登录后的openid，来向saas中心发起创建saas会员，如果存在则返回查询会员
        $url = "api/user/createUserByWeiXin";

        Log::info("开始 传递 " . $wxUserInfo['openid']);


        $result = SaasApiClient::send($url, array('wx_id' => $wxUserInfo['openid']));

        $result_str = serialize($result);

        Log::info("接口返回数据： {$result_str}");

        $manager = array();

        if ($result->isSucess()) {

            $user = $result->getData();

            //saas会员中心，user表
            $manager['id'] = $user['id'];//获取saasid
            $manager['username'] = $user['username'];//saas会员用户名
            $manager['name'] = $user['name'];//用户昵称
            $manager['from'] = $user['from'];//用户来源，1官网注册，2报价应用,3后台添加
            $manager['status'] = $user['status'];//用户状态，1启用，-1禁用
            $manager['addtime'] = $user['addtime'];//
            $manager['edittime'] = $user['edittime'];//
            $manager['wxId'] = $user['wx_id'];//微信id

            //微信公众号平台获取的微信信息
            $manager['wxNickName'] = $wxUserInfo['nickname'];//获取微信昵称
            $manager['headimgurl'] = $wxUserInfo['headimgurl'];//获取微信头像

            /*
             * 微信返回数据集
             {
   "openid":" OPENID",
   " nickname": NICKNAME,
   "sex":"1",
   "province":"PROVINCE"
   "city":"CITY",
   "country":"COUNTRY",
    "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
	"privilege":[
	"PRIVILEGE1"
	"PRIVILEGE2"
    ],
    "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
    }
             */
        } else {

            Log::info($result->getMessage());
            $manager = false;
        }

        Log::info("打印manager：" . json_encode($manager));


        if ($manager) {
            //存储session
            Session::start();

            $_SESSION[self::MANAGER_SESSION_NAME] = $manager;

            Log::info("打印_SESSION MANAGER_SESSION_NAME：" . json_encode($_SESSION[self::MANAGER_SESSION_NAME]));

        }
        return $manager;
    }

    /**
     * 获取登陆的管理员
     * @return mixed
     */
    public function getLoginManager()
    {
        Session::start();
        if (isset($_SESSION[self::MANAGER_SESSION_NAME])) {
            return $_SESSION[self::MANAGER_SESSION_NAME];
        } else {
            return false;
        }
    }

    public function setLoginManager($manager)
    {
        if (!empty($manager)) {
            Session::start();
            $_SESSION[self::MANAGER_SESSION_NAME] = $manager;
        }
    }

    public function setSession($key, $value = null)
    {
        if (is_array($key)) {
            Session::start();
            array_merge($_SESSION[self::MANAGER_SESSION_NAME], $key);
        } else {
            $_SESSION[self::MANAGER_SESSION_NAME][$key] = $value;
        }
    }

    public function getSession($key = null)
    {
        Session::start();
        if ($key) {
            return $_SESSION[self::MANAGER_SESSION_NAME][$key];
        } else {
            return $_SESSION[self::MANAGER_SESSION_NAME];
        }
    }

    /**
     * 判断当前用户是否登陆
     * @return bool
     */
    public function isLogin()
    {
        Session::start();

        Log::info("进入 isLogin ,打印_SESSION MANAGER_SESSION_NAME：" . json_encode($_SESSION[self::MANAGER_SESSION_NAME]));

        return isset($_SESSION[self::MANAGER_SESSION_NAME]);
    }

    /**
     * 登出
     * @return mixed
     */
    public function logout()
    {
        Session::start();
        $_SESSION = null;
        session_destroy();
    }
}
